#!/bin/sh

if [ "$1" == "--help" ] ; then
	echo
	echo " ------------------------------------------------------------------------------"
	echo "  format: sh $0 <phpXYm|phpXYs> [--force]"
	echo " ------------------------------------------------------------------------------"
	echo "  phpXYm/phpXYs"
	echo "      - start from php52m/php52s"
	echo "      - phpXYm for 'multiple php'"
	echo "      - phpXYs for 'special php' for Kloxo-ST panel"
	echo "  --force"
	echo "      - for re-install"
	echo
	exit;
fi

if [ "$#" == 0 ] ; then
	echo
	echo "- For help, type '$0 --help'"
	echo
	exit
fi

base_name="${1}"

if [ "echo '${base_name}'|grep 'php'|grep 's'" == "" ] || [ "echo '${base_name}'|grep 'php'|grep 'm'" == "" ] ; then
	echo "* WARNING: only for phpXYm or phpXYs (change XY to 52 - 70)"
	exit
fi

. /script/counter-start.inc

## ST -- remove last char (default m or s)
src_name="${base_name%?}"

if [ "${base_name}" == "php52s" ] ;  then
	yum remove php52s* -y
elif [ "${base_name}" == "php53s" ] ;  then
	yum remove php53s* -y
	'rpm' -e php53s-fpm --noscripts
elif [ "${base_name}" == "php54s" ] ;  then
	yum remove php54s* -y
	'rpm' -e php54s-fpm --noscripts
fi

mkdir -p /opt/${base_name} >/dev/null 2>&1

cd /opt/${base_name} >/dev/null 2>&1

rm -f /opt/${base_name}/install.log >/dev/null 2>&1

x="=== BEGIN '$1' install ==="
echo $x
echo $x >>/opt/${base_name}/install.log

if yum list ${src_name}u >/dev/null 2>&1 ; then
	pack_name="${src_name}u"
elif yum list ${src_name} >/dev/null 2>&1 ; then
	pack_name="${src_name}"
elif yum list ${src_name}w >/dev/null 2>&1 ; then
	pack_name="${src_name}w"
else
	echo "- No PHP packages exists. Only detecting '${src_name}u', '${src_name}' or '${src_name}w'. Exit"
	echo
	exit
fi

if [ "$(uname -m)" == "x86_64" ] ; then
	v=".x86_64"
else
	v=""
fi

if [ "$(rpm -qa yum-utils)" == "" ] ; then
	yum install yum-utils -y
fi

if [ -f /opt/${base_name}/${base_name} ] ; then
	## remove from previous process
	'rm' -f /opt/${base_name}/${base_name}
fi

pack_verrel=$(repoquery --qf='%{version}-%{release}' $pack_name)

if [ -f /opt/${base_name}/version ] ; then
	if [ "${2}" == "-y" ] || [ "${2}" == "--force" ] ; then
		echo "${pack_verrel}" > /opt/${base_name}/version
	else
		cver=$(cat /opt/${base_name}/version)

		if [ "$(rpm -qa rpmdevtools)" == "" ] ; then
			yum install rpmdevtools -y
		fi

		out=$(rpmdev-vercmp ${pack_verrel} ${cver})

		if [[ $out != *${pack_verrel}* ]] ; then
			x="- '${pack_name}-${pack_verrel}' ready to installed/updated"
			echo $x
			echo $x >>/opt/${base_name}/install.log
			. /script/counter-end.inc

			cd /
			sh /script/phpm-config-setup $base_name

			exit
		fi

		echo "${pack_verrel}" >/opt/${base_name}/version
	fi
else
	echo "${pack_verrel}" >/opt/${base_name}/version
fi

x="- Dependencies rpms... Please wait!"
echo $x
echo $x >>/opt/${base_name}/install.log

c=""

x="-- Check dependencies rpms"
echo $x
echo $x >>/opt/${base_name}/install.log

repoquery --qf='%{name}' --requires --resolve ${pack_name}-*|egrep -iv '(php|mysql|postgresql|mariadb|httpd|openssl)(.*)'|sort -u > /opt/${base_name}/dependencies

x="-- Install dependencies rpms"
echo $x
echo $x >>/opt/${base_name}/install.log
yum -y install $(cat /opt/${base_name}/dependencies) >>/opt/${base_name}/install.log

x="- Download all rpms (debug and devel will be deleted)... Please wait!"
echo $x
echo $x >>/opt/${base_name}/install.log

if [ -f /opt/${base_name}/etc ] ; then
	find /opt/${base_name}/etc ! \( -name "_*.ini" -or -name "*_used.ini" \) -exec rm -rf {} \;
fi

yumdownloader ${pack_name}-* >>/opt/${base_name}/install.log
'rm' -rf *-debug*
'rm' -rf *-devel*

x="- Extract all rpms to '/opt/${base_name}'"
echo $x
echo $x >>/opt/${base_name}/install.log

for i in $(ls ./*.rpm) ; do
	rpm2cpio $i | cpio -idmv >/dev/null 2>$1
done

'rm' -rf ./*.rpm

cd /
sh /script/phpm-config-setup $base_name

if [ -f /opt/${base_name}/${base_name} ] ; then
	'rm' -f /opt/${base_name}/${base_name}
fi

x="=== END '$1' install ==="
echo $x
echo $x >>/opt/${base_name}/install.log

. /script/counter-end.inc

echo

echo "Note:"
echo " - See '/opt/${base_name}/install.log' for detail install process"
echo " - Example: copy 'xcache.nonini' to '_xcache.ini' or 'xcache_used.ini'"
echo "      for enable 'xcache' module in '/etc/${base_name}/etc/php.d'"
echo " - For config setup only, run 'sh /script/phpm-config-setup ${base_name}"
echo
